package com.example.trb.controller.user;

import com.example.trb.dto.UserDTO;
import com.example.trb.entity.User;
import com.example.trb.entity.UserInfo;
import com.example.trb.entity.view.UserAllInfo;
import com.example.trb.service.user.UserService;
import com.example.trb.util.result.CommonResult;
import com.example.trb.util.result.SysReturnCode;
import com.example.trb.util.result.UserReturnCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
public class UserInfoController {
    @Autowired
    UserService userService;
    @Value("${session_user}")
    String userKey;

    /**
     * 修改用户信息
     *
     * @return
     */
    @PutMapping("/user/info")
    public CommonResult putUserInfo(@Validated @RequestBody UserAllInfo userInfo, HttpSession session) {
        User user = (User) session.getAttribute(userKey);

        Integer id = userService.getUserInfoId(user.getId());
        if (id == null) {
            return CommonResult.fail(400, "没有添加用户信息");
        }

        userInfo.setId(id);
        userInfo.setUserId(user.getId());
        userInfo.setName(user.getName());
        UserInfo info = transfer(userInfo);

        UserDTO dto = userService.setUserInfo(info);
        UserReturnCode code = dto.getCode();

        if (code == UserReturnCode.USER_NOT_EXIST) {
            return CommonResult.fail(code.code, code.message);
        }

        return CommonResult.success(dto.getData());
    }

    /**
     * 插入用户信息
     *
     * @param userAllInfo
     * @return
     */
    @PostMapping("/user/info")
    public CommonResult postUserInfo(@Validated @RequestBody UserAllInfo userAllInfo, HttpSession session) {
        User user = (User) session.getAttribute(userKey);
        userAllInfo.setUserId(user.getId());
        userAllInfo.setName(user.getName());
        UserInfo userInfo = transfer(userAllInfo);

        UserDTO info = userService.getUserAllInfo(user.getId());
        if (info.getData() != null) {
            SysReturnCode returnCode = SysReturnCode.BAD_REQUEST;
            return CommonResult.fail(returnCode.code, "已经存在一个用户信息");
        }

        UserDTO dto = userService.addUserInfo(userInfo);
        UserReturnCode code = dto.getCode();

        if (code == UserReturnCode.USER_NOT_EXIST) {
            return CommonResult.fail(code.code, code.message);
        }

        return CommonResult.success(dto.getData());
    }

    /**
     * 获取用户信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/user/simpleinfo")
    public CommonResult userSimpleInfo(@RequestParam("id") Integer userId) {
        UserDTO dto = userService.getUserAllInfo(userId);
        UserReturnCode code = dto.getCode();

        if (code == UserReturnCode.USER_NOT_EXIST) {
            return CommonResult.fail(code.code, "没有添加用户信息");
        }

        return CommonResult.success(dto.getData());
    }

    /**
     * 获取用户信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/user/detailinfo")
    public CommonResult userDetailInfo(@RequestParam("id") Integer userId) {
        UserDTO dto = userService.getUserAllInfo(userId);
        UserReturnCode code = dto.getCode();

        if (code == UserReturnCode.USER_NOT_EXIST) {
            return CommonResult.fail(code.code, "没有添加用户信息");
        }

        return CommonResult.success(dto.getData());
    }

    UserInfo transfer(UserAllInfo data) {
        return new UserInfo(
                data.getId(), data.getName(), data.getUserId(), data.getSex(),
                data.getNation(), data.getIdCardNumber(), data.getBirthday(),
                data.getDepartment(), data.getPoliticalAppearance(), data.getPersonalStatus(),
                data.getPhoneNumber(), data.getEmail(), data.getIdCardFrontPhoto(),
                data.getIdCardReversePhoto(), data.getOrigin(), data.getAddress(),
                data.getTwoInchPhoto(), data.getExecutive()
        );
    }
}
